<?php
//Author: Julian Villalta
//Homework #3

//Include the file with the array of quotes and source sites
require('quotes.php');

//Include the class
require('Get_Quote.php');

//Include the file that displays the header of the webpage
require('header.php');

//Create a new array
$arr_new_quote=array();

//Loop that instantiates the class and creates and obect for each quote and source site in the array
//stores each object in a new array
foreach($arr_quote as $arr_class_quote):
    $url_to_show= new Get_Quote($arr_class_quote['quote'], $arr_class_quote['source_site'] );
    $arr_new_quote[]= $url_to_show;
    unset($url_to_show);
endforeach;

//If their is a value in the GET array store that value in a variable
//eles store a random number in the variable between 0 and the number of values in the array
    if (isset($_GET['quote'])){
       $quote_number = $_GET['quote'];
    }
    else{
        $quote_number = rand(0,count($arr_new_quote)-1);
    }

//If the user inputs in the url a quote number that does not exist or an invalid character
//an error message will show
    if ( ! isset($arr_new_quote[$quote_number]))
        die("<h1>ERRROR!!!!! Quote does not exist!!!</h1>");

//if the user inputs ?list=true in the url it will show all the quotes and all source sites
//if not it will dispay the quote number that was either typed into the url or a random generated quote
    if(isset($_GET['list']) and $_GET['list'] == "true"){
        foreach($arr_new_quote as $arr_all_quotes):
            $the_quote = $arr_all_quotes->get_quote();
            $the_source_site = $arr_all_quotes->get_source_site();

            echo "<h1>\"" . $the_quote . "\"</h1>";
            echo "<p>" . $the_source_site  ;
        endforeach;
    }
    else{
        $curr_quote = $arr_new_quote[$quote_number];
        $the_quote = $curr_quote->get_quote();
        $curr_source_site = $arr_new_quote[$quote_number];
        $the_source_site = $curr_source_site->get_source_site();

        echo "<h1>\"" . $the_quote . "\"</h1>";
        echo "<p>" . $the_source_site  ;
    }

//Include the file containing the footer
require('footer.php');

